#include "sys_config.h"

//手机写入NFC的格式
/*
{"device_id":"smart_home","parameter":{"wifi_ssid":"Mate60Pro","wifi_passwd":"meiyoumima","client_ip":""}}
*/


/**
 * @brief 初始化串口、oled、nfc、wifi、TCP服务器
 * @note {*}
 * @param {*}
 * @return {*}
 */
static void device_init(void)
{
    
    /********************************** 外设初始化 **********************************/
    PCF8574_Init();// IO扩展芯片初始化
    AW2013_Init(); // 三色LED灯的初始化
    AW2013_Control_Red(0);
    AW2013_Control_Green(0);
    AW2013_Control_Blue(0);
    AP3216C_Init();    // 三合一传感器初始化
    SHT20_Init(); //温湿度传感器
    SSD1306_Init();
    SSD1306_CLS(); 
    key_init();
    // nfc_Init();     // NFC 初始化
    // nfc_connect_wifi_init();

    /*设备状态初始化*/
    globals_dev_status.lamp_state.lamp_power = POWER_STATUS_OFF;
    globals_dev_status.lamp_state.strength = 255;
    globals_dev_status.lamp_state.timer = 0;
    globals_dev_status.lamp_state.RGB_color_b = 0;
    globals_dev_status.lamp_state.RGB_color_g = 0;
    globals_dev_status.lamp_state.RGB_color_r = 0;

    globals_dev_status.beep_state.beep_power = POWER_STATUS_OFF;
    globals_dev_status.beep_state.frequency = 1;
    globals_dev_status.beep_state.timer = 0;

    globals_dev_status.fan_state.fan_power = POWER_STATUS_OFF;
    globals_dev_status.fan_state.speed = 1;
    globals_dev_status.fan_state.timer = 0;

    globals_dev_status.light = 0;
    globals_dev_status.temperature = 0.0;
    globals_dev_status.humidity = 1.5;
    globals_dev_status.infrared = 0;

    /********************************** 碰一碰联网 **********************************/
    configWifi();
    strcpy(globals_dev_status.local_ip_address, g_IP_Addr);

    /********************************** MQTT通信初始化 **********************************/
    // 设置订阅回调函数
    p_MQTTClient_sub_callback = &mqttClient_sub_callback;

    // 连接MQTT服务器
    if (MQTTClient_connectServer(SERVER_IP_ADDR, SERVER_IP_PORT) != WIFI_SUCCESS) {
        printf("[error] mqttClient_connectServer\r\n");
    }

    // 初始化MQTT客户端
    if (MQTTClient_init(MQTT_CLIENT_ID, MQTT_USER_NAME, MQTT_PASS_WORD) != WIFI_SUCCESS) {
        printf("[error] mqttClient_init\r\n");
    }

    // 订阅主题
    if (MQTTClient_subscribe(MQTT_TOPIC_SUB_COMMANDS) != WIFI_SUCCESS) {
        printf("[error] mqttClient_subscribe\r\n");
    }

}


Dev_status globals_dev_status; //定义在main.c
osMutexId_t globals_Mutex_ID = 0;
osMutexId_t uart_update_Mutex_ID = 0;

osThreadId_t Mqtt_Recv_Tackid = 0;
osThreadId_t Mqtt_Send_Tackid = 0;
osThreadId_t Dev_Control_Tackid = 0;


static void example_smarthome(void)
{
    printf("Enter example_smarthome()!\r\n");

    /*设备初始化*/
    device_init();

    // /*创建互斥锁*/
    // globals_Mutex_ID = osMutexNew(NULL);
    // if (globals_Mutex_ID != NULL) {
    //     printf("ID = %d, Create Mutex_ID is OK!\n", globals_Mutex_ID);
    // }

    // uart_update_Mutex_ID = osMutexNew(NULL);
    // if (uart_update_Mutex_ID != NULL) {
    //     printf("ID = %d, Create Mutex_ID is OK!\n", uart_update_Mutex_ID);
    // }

    /*创建任务*/
    osThreadAttr_t options;
    options.attr_bits = 0;
    options.cb_mem = NULL;
    options.cb_size = 0;
    options.stack_mem = NULL;
    options.stack_size = TASK_STACK_SIZE;
    
    //设备控制任务
    options.name = "Dev_Control_Tack";
    options.priority = osPriorityNormal1;
    Dev_Control_Tackid = osThreadNew((osThreadFunc_t)Dev_Control_Tack, NULL, &options);
    if (Dev_Control_Tackid != NULL) {
        printf("ID = %d, Create Dev_Control_Tack is OK!\r\n", Dev_Control_Tackid);
    }

    //MQTT发送任务
    options.name = "Mqtt_Send_Tack";
    options.priority = osPriorityNormal;
    Mqtt_Send_Tackid = osThreadNew((osThreadFunc_t)Mqtt_Send_Tack, NULL, &options);
    if (Mqtt_Send_Tackid != NULL) {
        printf("ID = %d, Create Mqtt_Send_Tack is OK!\r\n", Mqtt_Send_Tackid);
    }

    //MQTT接收任务
    options.name = "Mqtt_Recv_Tack";
    options.priority = osPriorityNormal;
    Mqtt_Recv_Tackid = osThreadNew((osThreadFunc_t)Mqtt_Recv_Tack, NULL, &options);
    if (Mqtt_Recv_Tackid != NULL) {
        printf("ID = %d, Create Mqtt_Recv_Tack is OK!\r\n", Mqtt_Recv_Tackid);
    }


}
SYS_RUN(example_smarthome);